<template>
    <div class="app-container">
        <!-- 补贴单信息 -->
        <h5 class="titleInfo">
            补贴单信息
        </h5>
        <el-form ref="list1" :inline="true" :model="list1" label-width="130px">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="流水单医院">
                        <span class="spanToInput">{{form.beOrderHospital | filterNull}}</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="补贴单医院">
                        <el-select placeholder="请选择" v-model="form.hospitalId">
                            <el-option
                                @click.native ="itemChange()"
                                v-for="item in hospOption"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目">
                        <el-select placeholder="请选择" v-model="form.projectId">
                            <el-option
                                v-for="item in itemOption"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="医生">
                        <el-select placeholder="请选择" v-model="form.doctorId">
                            <el-option
                                v-for="item in doctorOption"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目类型">
                        <el-select placeholder="请选择" v-model="form.projectType">
                            <el-option
                                v-for="(item,key) in itemTypeOption"
                                :key="key"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-form-item>
                    <el-button style="margin-top:30px;margin-left:130px" type="primary" @click="submitFn('form')" >立即提交</el-button>
                </el-form-item>
            </el-row>
        </el-form>
    </div>
</template>
<script>
import { hospitalDropDown } from '@/api/doctorManage'
import { findAllProjectListOfHospital,editInfo,findAllCategories,doEditInfo } from '@/api/subsidyManage'
import { listDoctorsDropdown } from '@/api/itemList'
export default {
    name:"subsidyManageEdit",
    data(){
        return {
            val:"1",
            list1:{},
            form:{},
            hospOption:[],
            itemOption:[],
            doctorOption:[],
            itemTypeOption:[],
            userId:null
        }
    },
    inject:['reload'],
    filters:{
        filterNull(e) {
            if(!e){
                return "暂无"
            }else{
                return e
            }
        },
        cardNull(e){
            return window.helper.cardType(e)
        },
        userFocus(e){
            return window.helper.userFocus(e)
        }
    },
    created(){
        this.userId = this.$route.query.id
        this.editInfo()
        this.getItemType()
    },
    methods:{
        editInfo(){
            let datas={
                id:Number(this.userId)
            }
            editInfo(datas).then(res=>{
                console.log(res)
                this.form = res.data.data
                // this.form.doctorId = Number(this.form.doctorId)
                // this.form.projectId = Number(this.form.projectId)
                // this.form.projectType = Number(this.form.projectType)
                console.log('projectId',this.form.projectId)
                this.getOption()
                this.items()
            })
        },
        getOption(){
            // 医院列表
            hospitalDropDown().then(res=>{
                let resData = res.data.data
                let newData = [];
                resData.forEach(e => {
                    newData.push({
                        value:Number(e.id),
                        label:e.name
                    })
                });
                this.hospOption = newData
                // this.item()
            })
            // this.getItemType()
        },
        getItemType(){
            // 获取项目类型
            let datas = {id:null}
            findAllCategories(datas).then(res=>{
                console.log(1111,res)
                let resData = res.data.data
                let newData = [];
                resData.forEach(item => {
                    newData.push({
                        value:item.name,
                        label:item.name
                    })
                });
                this.itemTypeOption = newData
                console.log(newData)
                // this.itemTypeOption.forEach(list=>{
                //     if(this.form.projectType ==list.value){
                //         this.form.projectType = list.label
                //     }
                // })
            })
        },
        items(){
            let datas = {
                id:Number(this.form.hospitalId)
            }
            findAllProjectListOfHospital(datas).then(res=>{
                console.log(res)
                let resData = res.data.data
                let newData = [];
                resData.forEach(e => {
                    newData.push({
                        value:Number(e.projectId),
                        label:e.name
                    })
                });
                this.itemOption = newData
                console.log(newData)
            })
            this.listDoctorsDropdown()
        },
        itemChange(){
            this.form.projectId = null
            this.form.doctorId = null
            this.form.projectType = null
            let datas = {
                id:Number(this.form.hospitalId)
            }
            findAllProjectListOfHospital(datas).then(res=>{
                console.log(res)
                let resData = res.data.data
                let newData = [];
                resData.forEach(e => {
                    newData.push({
                        value:Number(e.projectId),
                        label:e.name
                    })
                });
                this.itemOption = newData
                console.log(newData)
            })
            this.listDoctorsDropdown()
            this.getItemType()
        },
        listDoctorsDropdown(){
            let datas = {
                id:Number(this.form.hospitalId)
            }
            listDoctorsDropdown(datas).then(res=>{
                console.log(res)
                let resData = res.data.data
                let newData = [];
                resData.forEach(e => {
                    newData.push({
                        value:Number(e.id),
                        label:e.name
                    })
                });
                this.doctorOption = newData
                // console.log(res)
            })
        },
        submitFn(){
            this.form["id"] = Number(this.userId)
            console.log(this.form)
            let datas = this.form
            doEditInfo(datas).then(res=>{
                console.log(res)
                if(!res.data.code){
                    this.$message({
                        showClose: true,
                        message: '修改成功',
                        type: 'success'
                    });
                    this.$router.push('/subsidyManageIndex')
                    this.reload()
                }else{
                    this.$message({
                        showClose: true,
                        message: res.data.msg,
                        type: 'warning'
                    });
                }
            })
            .catch(error =>{
                this.$message.error(error);
            })
            
        }
    }
}
</script>
<style scoped>
    .app-container{padding: 0px!important}
    .divTable{padding: 0 20px 20px 20px;}
</style>